package kz.kstu.scantest.business.entity.data;

import kz.kstu.scantest.business.entity.Identifiable;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 10.09.11 15:38
 */
@Table(name = "SUBJECT_DATA")
@Entity
public class SubjectData implements Identifiable{

    private Long id;
    private FormData formData;
    private SubjectEnum subjectEnum;
    private List<AnswerData> answers;
    private FieldNameEnum fieldName;

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "FORM_DATA_ID", referencedColumnName = "ID")
    public FormData getFormData() {
        return formData;
    }

    public void setFormData(FormData formData) {
        this.formData = formData;
    }

    @Enumerated
    @Column (name = "SUBJECT_ID")
    public SubjectEnum getSubjectEnum() {
        return subjectEnum;
    }

    public void setSubjectEnum(SubjectEnum subjectEnum) {
        this.subjectEnum = subjectEnum;
    }

    public void addAnswer(AnswerData answerData) {
        if (answers == null) {
            answers = new ArrayList<AnswerData>();
        }
        answerData.setSubjectData(this);
        answers.add(answerData);
    }

    @OrderBy(value = "questionNumber")
    @OneToMany(mappedBy = "subjectData", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public List<AnswerData> getAnswers() {
        return answers;
    }

    public void setAnswers(List<AnswerData> answers) {
        this.answers = answers;
    }

    @Enumerated
    @Column (name = "FIELD_NAME_ID")
    public FieldNameEnum getFieldName() {
        return fieldName;
    }

    public void setFieldName(FieldNameEnum fieldName) {
        this.fieldName = fieldName;
    }
}
